Skip to content

feat(asyncpg): Add span streaming support#6215

Draft
ericapisani wants to merge 4 commits intomasterfrom
py-2305-migrate-asyncpg
Draft

feat(asyncpg): Add span streaming support#6215
ericapisani wants to merge 4 commits intomasterfrom
py-2305-migrate-asyncpg

Conversation

@ericapisani
Copy link
Copy Markdown
Member

@ericapisani ericapisani commented May 6, 2026

Migrates the asyncpg integration to support the stream trace lifecycle.

The integration now uses record_sql_queries_supporting_streaming and StreamedSpan so that DB spans are emitted as soon as they complete rather than being buffered until transaction end.

Tests are parameterized over both span_streaming=True and span_streaming=False to verify both paths.

Fixes PY-2305, fixes #6003

Migrates the asyncpg integration to use `record_sql_queries_supporting_streaming` and `StreamedSpan`, adding support for the `stream` trace lifecycle. Tests are parameterized to cover both static and streaming modes.

Fixes PY-2305
Fixes #6003
@linear-code
Copy link
Copy Markdown

linear-code Bot commented May 6, 2026

@ericapisani
Copy link
Copy Markdown
Member Author

bugbot run

@ericapisani
Copy link
Copy Markdown
Member Author

@sentry review

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 6, 2026

Codecov Results 📊

13 passed | Total: 13 | Pass Rate: 100% | Execution Time: 9.83s

📊 Comparison with Base Branch

Metric Change
Total Tests
Passed Tests
Failed Tests
Skipped Tests

✨ No test changes detected

All tests are passing successfully.

❌ Patch coverage is 8.16%. Project has 15073 uncovered lines.
❌ Project coverage is 31.33%. Comparing base (base) to head (head).

Files with missing lines (1)
File Patch % Lines
asyncpg.py 10.87% ⚠️ 123 Missing
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
- Coverage    31.35%    31.33%    -0.02%
==========================================
  Files          190       190         —
  Lines        21930     21950       +20
  Branches      7382      7390        +8
==========================================
+ Hits          6876      6877        +1
- Misses       15054     15073       +19
- Partials       581       581         —

Generated by Codecov Action

Comment thread sentry_sdk/integrations/asyncpg.py
Comment thread sentry_sdk/integrations/asyncpg.py
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit fd3ee4e. Configure here.

Comment thread sentry_sdk/integrations/asyncpg.py Outdated
Comment thread sentry_sdk/integrations/asyncpg.py
…an has been created. This allows the traces_sampler to see the attributes and make decisions based on them
Comment on lines -86 to -87
with capture_internal_exceptions():
add_query_source(span)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sentrivana I realize this code hasn't been touched in 2 years, but since you were the last author - I'm wondering if you can recall why this is sitting outside the record_sql_queries context manager - this looks like it should be sitting within it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the streaming path, for sure, because it sets attributes on the span record_sql_queries creates, and that span needs to still be alive for us to do that.

On the legacy path, it's the other way around, we need the span to be finished (since we use its end timestamp to calculate the duration) to decide whether query source should be attached (because the span is over the configured threshold).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrate asyncpg to span first

2 participants